package y24.m3;

import java.util.*;

public class Test7 {
    static class Student{
        private String name;
        private Integer[] kScore = new Integer[4];
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Map<String,Integer> k = new HashMap<>();
        k.put("Java程序设计",0);
        k.put("数据库原理",1);
        k.put("数据结构",2);
        k.put("算法设计与分析",3);
        List<Student> studentList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            System.out.printf("请输入第%d名学生姓名：",i+1);
            Student student = new Student();
            student.name = scanner.nextLine();
            for (int j = 0; j < 4; j++)
                student.kScore[j] = getScore();
            studentList.add(student);

        }
        int i;
        while (true){
            menu();
            i = scanner.nextInt();
            switch (i){
                case 1:
                    System.out.print("请输入学生姓名：");
                    studentScore(studentList,k);
                    break;
                case 2:
                    System.out.print("请输入课程名称：");
                    keScore(studentList,k);
                    break;
                case 3:
                    return;
                default:
                    System.out.println("输入不合法！请重新输入");
                    break;
            }
        }
    }

    private static void studentScore(List<Student> studentList, Map<String, Integer> k) {
        String studentName = new Scanner(System.in).nextLine();
        for (int i = 0; i < studentList.size(); i++) {
            if (studentName.equals(studentList.get(i).name)) {
                System.out.print(studentName + " ");
                for (String key : k.keySet()) {
                    System.out.print("  " + key + "：" + studentList.get(i).kScore[k.get(key)]);
                }
            }
        }
        System.out.println();
    }
    private static void keScore(List<Student> studentList, Map<String, Integer> k) {
        String kName = new Scanner(System.in).nextLine();
        double sum = 0;
        int maxS = 0;
        int minS = 100;
        int count = 0;
        System.out.print("不及格的人：");
        for (int i = 0; i < studentList.size(); i++) {
            int score = studentList.get(i).kScore[k.get(kName)];
            if (score < 60){
                System.out.print(" " + studentList.get(i).name);
                count++;
            }
            sum += score;
            maxS = Math.max(maxS,score);
            minS = Math.min(minS,score);
        }
        if (count == 0){
            System.out.print( "无");
        }
        double p = sum / 6;
        System.out.println("平均分：" + p + " 最高分：" + maxS + " 最低分：" + minS);
    }
    private static void menu() {
        System.out.println("==========================");
        System.out.println("1、查询学生各科成绩");
        System.out.println("2、查询不及格学生，平均分，最高分，最低分");
        System.out.println("3、退出");
        System.out.println("==========================");
    }
    private static Integer getScore() {
        return new Random().nextInt(100);
    }
}